package com.tsd.core.excel.impexcel;


import com.tsd.core.utils.*;
import com.tsd.core.vo.HlpException;
import com.tsd.core.utils.HlpUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Excel文件配置
 *
 * @author wudw
 */
public class ExcelConfigFile implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1339934976606197339L;

    private List<ExcelConfig> items;

    private int sheetIndex;
    private int dataStartRow;
    /**
     * 列数量
     */
    private int columnCount;
    /**
     * 出错信息写入的列index
     */
    private int errColIndex;
    /**
     * 最后忽略行数
     */
    private Integer lastRowSkipQty = 0;
    private String password;

    public static ExcelConfigFile getInstance() {
        return new ExcelConfigFile();
    }

    public String init(String fileName) throws Exception {
        items = new ArrayList<>();
        String configPath = ResourcesUtil.getExcelImportPath(fileName);
        if (!FileUtil.isFileExist(configPath)) {
            throw new HlpException("配置文件不存在" + configPath);
        }
        Map<String, String> paramsMap = ConfigUtil.loadMapFromXML(configPath, false, true);
        Set<String> keys = paramsMap.keySet();
        StringBuilder buf = new StringBuilder();
        errColIndex = 0;
        for (String key : keys) {
            String v = paramsMap.get(key);
            if ("sheetIndex".equalsIgnoreCase(key)) {
                if (StringUtil.isInteger(v)) {
                    this.setSheetIndex(Integer.parseInt(v));
                } else {
                    this.setSheetIndex(0);
                }
                continue;
            }
            if ("dataStartRow".equalsIgnoreCase(key)) {
                if (StringUtil.isInteger(v)) {
                    this.dataStartRow = Integer.parseInt(v);
                } else {
                    this.dataStartRow = 1;
                }
                continue;
            }
            if ("lastRowSkipQty".equalsIgnoreCase(key)) {
                if (StringUtil.isInteger(v)) {
                    this.lastRowSkipQty = Integer.parseInt(v);
                } else {
                    this.lastRowSkipQty = 0;
                }
                continue;
            }
            String[] ps = v.split(",");
            ExcelConfig ec = new ExcelConfig();
            String psIndex = null;
            if (ps.length > 0) {
                psIndex = ps[0];
            }
            String psType = null;
            if (ps.length > 1) {
                psType = ps[1];
            }
            String psLen = null;
            if (ps.length > 2) {
                psLen = ps[2];
            }
            String psMust = null;
            if (ps.length > 3) {
                psMust = ps[3];
            } else {
                psMust = "0";
            }
            String psHide = null;
            if (ps.length > 4) {
                psHide = ps[4];
            } else {
                psHide = "0";
            }
//            if (StringUtil.isInteger(psIndex)) {
//                int index = Integer.parseInt(psIndex);
//                if (index > errColIndex) {
//                    //获取当前最大的列index
//                    errColIndex = index;
//                }
//                ec.setCellIndex(index);
//            } else {
//                buf.append(key).append("的位置不是数字\n");
//            }
            if (HlpUtils.isEmpty(psIndex)) {
                buf.append(key).append("的位置不能为空\n");
            } else {
                try {
                    int index = ExcelFileUtil.transLetter2Num(psIndex);
                    if (index > errColIndex) {
                        //获取当前最大的列index
                        errColIndex = index;
                    }
                    ec.setCellIndex(index);
                    ec.setCellLetter(psIndex);
                } catch (Exception e) {
                    buf.append(key).append("：").append(e.getMessage()).append("\n");
                }
            }
            ec.setFieldName(key);
            if (HlpUtils.isEmpty(psType)) {
                buf.append(key).append("的类型不能为空\n");
            } else {
                ec.setFieldType(psType);
            }
            if (!HlpUtils.isEmpty(psLen)) {
                ec.setLength(Integer.parseInt(psLen));
            } else {
                if (!HlpUtils.isEmpty(psType) && "string".equalsIgnoreCase(psType)) {
                    buf.append("长度不能为空，非字符串的长度设置为0\n");
                }
            }
            if (!HlpUtils.isEmpty(psMust)) {
                ec.setMust("1".equals(psMust));
            } else {
                buf.append(key).append("是否必填需要设置\n");
            }
            if (!HlpUtils.isEmpty(psHide)) {
                ec.setHide("1".equals(psHide));
            } else {
                buf.append(key).append("是否隐藏需要设置\n");
            }
            items.add(ec);
        }
        //设置errColIndex为最大数据列的后一列
        errColIndex++;
        columnCount = errColIndex;
        return buf.toString();
    }

    public List<ExcelConfig> getItems() {
        return items;
    }

    public void setItems(List<ExcelConfig> items) {
        this.items = items;
    }

    public int getSheetIndex() {
        return sheetIndex;
    }

    public void setSheetIndex(int sheetIndex) {
        this.sheetIndex = sheetIndex;
    }

    public int getDataStartRow() {
        return dataStartRow;
    }

    public void setDataStartRow(int dataStartRow) {
        this.dataStartRow = dataStartRow;
    }

    public int getColumnCount() {
        return columnCount;
    }

    public void setColumnCount(int columnCount) {
        this.columnCount = columnCount;
    }

    public int getErrColIndex() {
        return errColIndex;
    }

    public void setErrColIndex(int errColIndex) {
        this.errColIndex = errColIndex;
    }

    public Integer getLastRowSkipQty() {
        return lastRowSkipQty;
    }

    public void setLastRowSkipQty(Integer lastRowSkipQty) {
        this.lastRowSkipQty = lastRowSkipQty;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
